package org.jflame.captcha.creator.factory;

import org.jflame.captcha.RandomImgManager;
import org.jflame.captcha.creator.CaptchaCreator;
import org.jflame.captcha.creator.WordClickCaptchaCreator;
import org.jflame.commons.config.ConfigReader;
import org.jflame.commons.config.ConfigKey;

import java.awt.Font;
import java.io.IOException;

@SuppressWarnings("rawtypes")
public class WordClickCreatorFactory extends CaptchaCreatorFactory {

    /**
     * 文字生成个数
     */
    public ConfigKey<Integer> KEY_COUNT = new ConfigKey<>(cfgKey("count"), 4);
    /**
     * 随机文字是否生成汉字
     */
    public ConfigKey<Boolean> KEY_HAS_CHINESE = new ConfigKey<>(cfgKey("has_chinese"), true);
    /**
     * 随机文字是否生成数字
     */
    public ConfigKey<Boolean> KEY_HAS_NUMBER = new ConfigKey<>(cfgKey("has_number"), false);
    /**
     * 随机文字是否生成字母
     */
    public ConfigKey<Boolean> KEY_HAS_LETTER = new ConfigKey<>(cfgKey("has_letter"), false);
    /**
     * 生成的图片宽度
     */
    public ConfigKey<Integer> KEY_WIDTH = new ConfigKey<>(cfgKey("width"), 200);
    /**
     * 生成的图片高度
     */
    public ConfigKey<Integer> KEY_HEIGTH = new ConfigKey<>(cfgKey("heigth"), 120);
    /**
     * 字体名称,如果以file:或classpath:开头表示字段文件路径
     */
    public ConfigKey<String> KEY_FONT = new ConfigKey<>(cfgKey("font"));
    /**
     * 字体尺寸
     */
    public ConfigKey<Integer> KEY_FONT_SIZE = new ConfigKey<>(cfgKey("font_size"), 24);
    /**
     * 文字点选:是否加入混淆字
     */
    public static ConfigKey<Boolean> KEY_WORDCLICK_MISLEAD = new ConfigKey<>("captcha.word_click.mislead", false);
    /**
     * 文字点选:背景图目录
     */
    public static ConfigKey<String> KEY_WORDCLICK_BG_DIR = new ConfigKey<>("captcha.word_click.bg_dir");
    /**
     * 文字点选:筛选最大背景图数量
     */
    public static ConfigKey<Integer> KEY_WORDCLICK_BG_MAX_FILES = new ConfigKey<>("captcha.word_click.bg_max_files",
            20);

    @Override
    public CaptchaCreator build(ConfigReader captchaConfig) {
        RandomImgManager imgManager;
        try {
            imgManager = new RandomImgManager(captchaConfig.getString(KEY_WORDCLICK_BG_DIR),
                    captchaConfig.getInt(KEY_WORDCLICK_BG_MAX_FILES));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        WordClickCaptchaCreator creator = new WordClickCaptchaCreator(imgManager);
        if (captchaConfig.hasKey(KEY_COUNT.getName())) {
            creator.getWordGenerator()
                    .setCount(captchaConfig.getInt(KEY_COUNT));
        }

        if (captchaConfig.getBoolean(KEY_WORDCLICK_MISLEAD)) {
            creator.enableMislead();
        }
        creator.setWidth(captchaConfig.getInt(KEY_WIDTH));
        creator.setHeight(captchaConfig.getInt(KEY_HEIGTH));
        Font font = loadFont(captchaConfig.getString(KEY_FONT), captchaConfig.getInt(KEY_FONT_SIZE));
        if (font != null) {
            creator.setDefaultFont(font);
        }
        return creator;
    }

    @Override
    public String getCaptchaType() {
        return "word_click";
    }

}
